﻿using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ScanSnap.Domain
{
    public class Database
    {
        public List<Organization> OrganizationList { get; set; }
        public ObservableCollection<Patient> PatientList { get; set; }
        public ObservableCollection<Tag> TagList { get; set; }

        public int LoginType { get; set; }

        public Database(int type)
        {
            this.LoginType = type;
            OrganizationList = new List<Organization>();
            AddOrganizations();
            AddPatients();
            AddTags(type);
        }

        private void AddOrganizations()
        {
            if (LoginType == 1)
            {
                OrganizationList.Add(new Organization { Id = "", Name = "選択してください" });
                OrganizationList.Add(new Organization { Id = "H01", Name = "あかみち薬局" });
                OrganizationList.Add(new Organization { Id = "H02", Name = "さんだん花ガーデン" });
                OrganizationList.Add(new Organization { Id = "H03", Name = "統合医療センター" });
            }
            else
            {
                OrganizationList.Add(new Organization { Id = "", Name = "選択してください" });
                OrganizationList.Add(new Organization { Id = "R01", Name = "Risk (Naha)" });
                OrganizationList.Add(new Organization { Id = "R03", Name = "Risk (Omoromachi)" });
                OrganizationList.Add(new Organization { Id = "R02", Name = "Risk (Uruma)" });
            }
        }

        private void AddPatients()
        {
            //Patient p11 = new Patient { Id = "P01", Name = "貝原良治", DataOfBirth = "1946年3月11日 （67歳）", Sex = "男", Address = "沖縄県那覇市西2-16-3　屋島組本社ビル2－A号室", DocumentPath="1.pdf" };
            //Patient p12 = new Patient { Id = "P02", Name = "貝淵俊二", DataOfBirth = "1941年11月16日 （72歳）", Sex = "男", Address = "東京都中央区日本橋箱崎町5-11 ユニバーサルビル5F", DocumentPath = "2.pdf" };
            //Patient p13 = new Patient { Id = "P03", Name = "最相葉月", DataOfBirth = "1939年6月13日 （74歳）", Sex = "女", Address = "沖縄県那覇市西2-16-3　屋島組本社ビル2－A号室", DocumentPath = "3.pdf" };

            //Patient p21 = new Patient { Id = "P04", Name = "海江田万里", DataOfBirth = "1952年5月1日 （61歳）", Sex = "男", Address = "沖縄県那覇市西2-16-3　屋島組本社ビル2－A号室", DocumentPath = "4.pdf" };
            //Patient p22 = new Patient { Id = "P05", Name = "魁皇博之", DataOfBirth = "1958年2月12日 （55歳）", Sex = "女", Address = "東京都中央区日本橋箱崎町5-11 ユニバーサルビル5F", DocumentPath = "5.pdf" };

            //Patient p31 = new Patient { Id = "P06", Name = "海崎洋一郎", DataOfBirth = "1941年5月21日 （72歳）", Sex = "女", Address = "沖縄県那覇市西2-16-3　屋島組本社ビル2－A号室", DocumentPath = "6.pdf" };
            //Patient p32 = new Patient { Id = "P07", Name = "貝塚啓明", DataOfBirth = "1943年5月11日 （70歳）", Sex = "男", Address = "東京都中央区日本橋箱崎町5-11 ユニバーサルビル5F", DocumentPath = "7.pdf" };
            //Patient p33 = new Patient { Id = "P08", Name = "海渡英祐", DataOfBirth = "1949年5月7日 （64歳）", Sex = "女", Address = "沖縄県那覇市西2-16-3　屋島組本社ビル2－A号室", DocumentPath = "8.pdf" };

            //Patient p41 = new Patient { Id = "P09", Name = "貝原俊民", DataOfBirth = "1948年2月1日 （65歳）", Sex = "女", Address = "東京都中央区日本橋箱崎町5-11 ユニバーサルビル5F", DocumentPath = "9.pdf" };

            //PatientList = new ObservableCollection<Patient>();

            //PatientList.Add(p11);
            //PatientList.Add(p12);
            //PatientList.Add(p13);
            //PatientList.Add(p21);
            //PatientList.Add(p22);
            //PatientList.Add(p31);
            //PatientList.Add(p32);
            //PatientList.Add(p33);
            //PatientList.Add(p41);

            //OrganizationList[0].AddPatient(p11);
            //OrganizationList[0].AddPatient(p12);
            //OrganizationList[0].AddPatient(p13);

            //OrganizationList[1].AddPatient(p21);
            //OrganizationList[1].AddPatient(p22);

            //OrganizationList[2].AddPatient(p31);
            //OrganizationList[2].AddPatient(p32);
            //OrganizationList[2].AddPatient(p33);

            //OrganizationList[2].AddPatient(p41);
        }

        private ObservableCollection<Tag> AddTags(int category)
        {
            TagList = new ObservableCollection<Tag>();

            switch(category)
            {
                case 0:
                    break;
                case 1:
                        TagList.Add(new Tag { Id = "T01", Name = "あいうえお" });
                        TagList.Add(new Tag { Id = "T02", Name = "かきくけこ" });
                        TagList.Add(new Tag { Id = "T03", Name = "さしすせそ" });
                        TagList.Add(new Tag { Id = "T04", Name = "たちつてと0" });
                        break;
                case 2:
                        TagList.Add(new Tag { Id = "T01", Name = "あいうえお" });
                        TagList.Add(new Tag { Id = "T02", Name = "かきくけこ" });
                        TagList.Add(new Tag { Id = "T03", Name = "さしすせそ" });
                        TagList.Add(new Tag { Id = "T04", Name = "たちつてと" });
                        break;
                default:
                        TagList.Add(new Tag { Id = "T01", Name = "あいうえお" });
                        TagList.Add(new Tag { Id = "T02", Name = "かきくけこ" });
                        TagList.Add(new Tag { Id = "T03", Name = "さしすせそ" });
                        TagList.Add(new Tag { Id = "T04", Name = "たちつてと" });
                        break;
            }

            return TagList;
        }


        public ObservableCollection<Tag> FindByTagGroup(int type)
        {
            AddTags(type);
            return this.TagList;
        }

        public ObservableCollection<Patient> FindPatientsByOrganizationId(string id)
        {
            Organization org = FindOrganizationById(id);
            if (org != null)
                return org.Patients;
            else
                return null;
        }

        public Organization FindOrganizationById(string id)
        {
            foreach (Organization org in OrganizationList)
            {
                if(org.Id.Equals(id) )
                    return org;
            }

            return null;
        }

        public Patient FindPatientById(string id)
        {
            foreach (Patient p in PatientList)
            {
                if(p.Id.Equals(id))
                    return p;
            }

            return null;
        }

        public Patient FindPatientByName(string name)
        {
            foreach (Patient p in PatientList)
            {
                if (p.Name.StartsWith(name))
                    return p;
            }

            return null;
        }

        public Organization FindByOrganizationId(string id)
        {
            foreach (Organization o in OrganizationList)
            {
                if (o.Id.Equals(id))
                {
                    return o;
                }
            }

            return null;
        }

        public Organization FindByOrganizationName(string name)
        {
            foreach (Organization org in OrganizationList)
            {
                if (org.Name.StartsWith(name))
                    return org;
            }

            return null;
        }
    }
}
